//==============================================================================
//
//               Copyright (C) 2004  Dick van Oudheusden
//  
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, write to the Free
// Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
//==============================================================================
// 
//  $Date: 2004-10-01 06:23:38 $ $Revision: 1.4 $
//
//==============================================================================

#include <stdlib.h>

#include "ofc/config.h"

#include "ofc/DColor.h"

#include "DInc.h"
#include "DTest.h"

//-Datatype--------------------------------------------------------------------

void DColor_test()
{
  DColor        *color1 = [DColor new];
  DColor        *color2 = [DColor alloc];
  DColor        *color3 = [DColor alloc];
  DColor        *color4 = [DColor alloc];
  double        R,G,B;
  double        H,L,S,V;
  double        Y,I,Q;
  double        C,M;
  unsigned char red,green,blue;
  char          test1[] = "#FF8023";
  char          test2[] = "25%,12%,34%,yellow";
  char          test3[] = "0.3,0.23,0.9";
  char          test4[] = "2,22,222,green";
  char          *pntr;
  
  STARTTEST();

  // Constructors
  TEST([color1 red  ]     == 0);
  TEST([color1 green]     == 0);
  TEST([color1 blue ]     == 0);
  TEST([color1 alpha]     == 255);
  TEST([color1 textColor] == DCLR_BLACK);

  [color2 init :"MAROON"];
  TEST([color2 red  ]     == 176);
  TEST([color2 green]     == 48);
  TEST([color2 blue ]     == 96);
  TEST([color2 alpha]     == 255);
  TEST([color2 textColor] == DCLR_BLUE);

  [color3 init :24 :31 :253];
  TEST([color3 red  ]     == 24);
  TEST([color3 green]     == 31);
  TEST([color3 blue ]     == 253);
  TEST([color3 alpha]     == 255);
  TEST([color3 textColor] == DCLR_BLACK);
  
  [color4 init :254 :0 :121 :30];
  TEST([color4 red  ]     == 254);
  TEST([color4 green]     == 0);
  TEST([color4 blue ]     == 121);
  TEST([color4 alpha]     == 30);
  TEST([color4 textColor] == DCLR_BLACK);
  
  // Setters
  [color4 set :192 :129 :219 :60 :DCLR_YELLOW];
  TEST([color4 red  ]     == 192);
  TEST([color4 green]     == 129);
  TEST([color4 blue ]     == 219);
  TEST([color4 alpha]     == 60);
  TEST([color4 textColor] == DCLR_YELLOW);

  TEST([[color4 alpha :53] alpha] == 53);
  TEST([[color4 textColor :DCLR_RED] textColor] == DCLR_RED);
  
  // Color manipulation
  [color1 set :"olive"];
  
  [color1 lighter :1.1];
  TEST([color1 red  ]     == 94); 
  TEST([color1 green]     == 118);
  TEST([color1 blue ]     == 52);
  
  [color1 lighter :0.8];
  TEST([color1 red  ]     == 75);
  TEST([color1 green]     == 94);
  TEST([color1 blue ]     == 42);

  [color1 set  :"red"];
  [color1 blend :0 :0 :255 :30];
  TEST([color1 red  ]     == 29);
  TEST([color1 green]     == 0);
  TEST([color1 blue ]     == 226);
  
  // Color systems
  [color1 fromRGB :0.4 :0.6 :0.8];
  
  red   = [color1 red  ];
  green = [color1 green];
  blue  = [color1 blue ];
  
  TEST(red   == 102);
  TEST(green == 153);
  TEST(blue  == 204);
  
  [color1 toRGB :&R :&G :&B];
  TEST(R == 0.4);
  TEST(G == 0.6);
  TEST(B == 0.8);
  
  [color1 toHLS   :&H :&L :&S];
  [color1 fromHLS : H : L : S];
  
  TEST([color1 red  ] == red  );
  TEST([color1 green] == green);
  TEST([color1 blue ] == blue );
  
  [color1 toHSV   :&H :&S :&V];
  [color1 fromHSV : H : S : V];

  TEST([color1 red  ] == red  );
  TEST([color1 green] == green);
  TEST([color1 blue ] == blue );

  [color1 toYIQ   :&Y :&I :&Q];
  [color1 fromYIQ : Y : I : Q];
  
  TEST([color1 red  ] == red  );
  TEST([color1 green] == green);
  TEST([color1 blue ] == blue );

  [color1 toCMY   :&C :&M :&Y];
  [color1 fromCMY : C : M : Y];
  
  TEST([color1 red  ] == red  );
  TEST([color1 green] == green);
  TEST([color1 blue ] == blue );
  
  // Textable protocol
  TEST([[color1 toText] ccompare :"6699CC,red"] == 0);
  
  // Parsable protocol
  pntr = test1;
  
  TEST([color1 fromString :&pntr] == 0);
  
  TEST(*pntr == EOS);
  
  TEST([color1 red  ] == 255);
  TEST([color1 green] == 128);
  TEST([color1 blue ] == 35);
  
  
  pntr = test2;
  
  TEST([color1 fromString :&pntr] == 0);
  
  TEST(*pntr == EOS);

  TEST([color1 red  ] == 64);
  TEST([color1 green] == 31);
  TEST([color1 blue ] == 87);
  TEST([color1 textColor] == DCLR_YELLOW);
  
  
  pntr = test3;
  
  TEST([color1 fromString :&pntr] == 0);
  
  TEST([color1 red  ] == 76);
  TEST([color1 green] == 59);
  TEST([color1 blue ] == 230);

  
  pntr = test4;
  
  TEST([color1 fromString :&pntr] == 0);
  
  TEST([color1 red  ] == 2);
  TEST([color1 green] == 22);
  TEST([color1 blue ] == 222);
  TEST([color1 textColor] == DCLR_GREEN);
  
  [color1 free];
  [color2 free];
  [color3 free];
  [color4 free];
  
  STOPTEST();
}

